(c) 2001 Visual Basic Programmer's Journal
Fawcette Technical Publications

Issue: February 2001
Section: Getting Started
Authors: Rosebrock and Schultes

VB 5,6	Retrieve an Image From the Database
Listing 1 Retrieve a record from the database and propagate the fields on the form. If the image path is empty, the program assumes that the image is stored as a BLOB. The procedure assumes that you've opened the recordset (rs) and positioned it to the correct record.

Public Sub FillFields()
'Propagate fields with record data

Dim lngImageSize As Long
Dim lngOffset As Long
Dim bytChunk() As Byte
Dim intFile As Integer
Dim strTempPic As String
Const conChunkSize = 100
Dim strImage As String

If Not (rs.BOF And rs.EOF) Then
	txtImageTitle.Text = _
		rs.Fields("ImageTitle")
		
	If Trim$("" & _
		rs.Fields("ImagePath")) = "" Then 
		'Image saved as a BLOB
		txtImagePath.Text = ""
		optImageType(1).Value = True

		'Make sure the temporary file 
		'does not already exist
		strTempPic = App.Path & "\TempPic.jpg"
		If Len(Dir(strTempPic)) > 0 Then
			Kill strTempPic
		End If

		'Open the temporary file to 
		'save the BLOB to
		intFile = FreeFile
		Open strTempPic For Binary As #intFile

		'Read the binary data into 
		'the byte variable array
		lngImageSize = _
			rs("ImageBLOB").ActualSize
		Do While lngOffset < lngImageSize
			bytChunk() = rs _
				("ImageBLOB").GetChunk(conChunkSize)
			Put #intFile, , bytChunk()
			lngOffset = lngOffset + conChunkSize
		Loop
		Close #intFile

		'After loading the image, get 
		'rid of the temporary file
		imgDBImage.Picture = _
			LoadPicture(strTempPic)
		Kill strTempPic
	Else 'Image saved as file pointer
		txtImagePath.Text = rs.Fields("ImagePath")
		optImageType(0).Value = True
		If Not IsNull(rs.Fields ("ImagePath")) Then
			strImage = Trim$("" & _
				rs.Fields("ImagePath"))
			If Len(Dir(strImage)) Then
				imgDBImage.Picture = _
					LoadPicture(strImage)
			Else
				imgDBImage.Picture = LoadPicture()
			End If
		End If
	End If
End If
End Sub

VB 5,6	Save the Image
Listing 2 Save the record to the database. Depending on the option button selected, save the image to the database as either a file pointer or as a BLOB. The procedure assumes that you either positioned the recordset (rs) to a new record, or that you're editing the current record (rs.Edit).

Private Sub SaveToDB()
Dim bytBLOB() As Byte
Dim strImagePath As String
Dim intNum As Integer
	
'Save the record
strImagePath = Trim$(txtImagePath.Text)
With rs
	.Fields("ImageTitle") = _
		Trim$(txtImageTitle.Text)
		
	If (optImageType(0).Value) Then 
		'Save as file pointer
		.Fields("ImagePath") = strImagePath
	Else
		If (txtImagePath.Text <> "") Then
			'Open the picture file
			intNum = FreeFile
			Open strImagePath For Binary As #intNum
			ReDim bytBLOB (FileLen(strImagePath))
			'Read data and close file
			Get #intNum, , bytBLOB
			Close #1
				
			'Store the BLOB
			.Fields("ImagePath") = ""
			.Fields("ImageBLOB"). _
				AppendChunk bytBLOB
		End If
	End If
	.Update
End With
End Sub
